//
//  ViewController.swift
//  ShopCarDemo
//
//  Created by Rookie_YX on 16/6/21.
//  Copyright © 2016年 YX_Rookie. All rights reserved.
//

import UIKit
import SnapKit

class ViewController: UIViewController , UITableViewDelegate,UITableViewDataSource{
    
    // show  hidden
    var type: ButtonType = .hidden
    var clickedRowArr:[NSInteger] = []
    var isClickeRow = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.whiteColor()
        setupUI()
    }
    
    // MARK:- 自定义
    private func setupUI()
    {
        view.addSubview(tableView)
        view.addSubview(orderBar)
        orderBar.snp_makeConstraints { (make) in
            make.bottom.left.right.equalTo(view)
            make.height.equalTo(44)
        }
        
    }
    // MARK:- 懒加载
    
    private lazy var tableView:UITableView = {
        
        let tab = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
        tab.delegate = self
        tab.dataSource = self
        tab.separatorStyle = UITableViewCellSeparatorStyle.None
        return tab
    }()
    
    
    private lazy var orderBar:OrderBarView = {
        let orderBar = OrderBarView()
        return orderBar
    }()
}

extension ViewController{
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return 10
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        var cell = tableView.dequeueReusableCellWithIdentifier("cell")  as? OrderCell
        if cell == nil {
            cell = OrderCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell", type: self.type)
        }
        if self.type == ButtonType.hidden {
            cell?.shopTitleL.text = String("选购商品\(indexPath.row)")
        }
        if self.type == ButtonType.show{
            cell?.shopTitleL.text = String("你选中了商品\(indexPath.row)")
        }
        return cell!
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        isClickeRow = !isClickeRow
        self.clickedRowArr.append(indexPath.row)
        self.type = ButtonType.show
        tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Middle)
    }
}

